<template>
  <div :ref="id||'main'" :id="id" style="width: 100%; height: 100%"></div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'LineCharts',
  props: {
    chartData: {
      type: Array,
      default: () => []
    },
    id: {
      type: String,
      default: 'main'
    },
    color:{
      type: String,
      default: 'rgb(0, 171, 134)'
    }
  },
  watch: {
    chartData: {
      handler(newValue, oldValue) {
        if (newValue != oldValue) {
          setTimeout(() => {
            this.init(newValue)
          }, 100)
        }
      },
      immediate: true
    }
  },
  data() {
    return {}
  },
  methods: {
    init(data) {
      var chartDom = this.$refs[this.id] //document.getElementById(this.id || 'main')
      // 获取实例
      var myChart = echarts.getInstanceByDom(chartDom);

      if (!myChart) // 如果不存在则创建
      {
        myChart = echarts.init(chartDom);
      }
      // var myChart = echarts.init(chartDom)
      var option

      option = {
        tooltip: {
          trigger: 'axis',
          showContent: false
        },
        xAxis: {
          type: 'category',
          show: false,
          boundaryGap: false,
        },
        yAxis: {
          type: 'value',
          show: false,
          boundaryGap: [0, '100%']
        },
        series: [
          {
            data,
            type: 'line',
            itemStyle: {
              color: this.color,
              width: 4
            },
            // symbolSize: 9,
            showSymbol: false,
            areaStyle: {
              // color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              //   {
              //     offset: 0,
              //     color: 'rgb(0, 171, 134)'
              //   },
              //   {
              //     offset: 1,
              //     color: 'rgb(123,248,221)'
              //   }
              // ])
            },

            smooth: true,
            // symbol: 'none',
          }
        ]
      }
      option && myChart.setOption(option)
    }
  },
  components: {}
}
</script>

<style></style>
